import { defineStore } from 'pinia'

/**
 * @应用仓库
 */

export const useAppStore = defineStore('app', {
  state: () => ({
    isMobile: true,
    theme: 'light', // light dark
    style: 'default', // default gray red
    styleList: [
      { value: 'default', label: '默认' },
      { value: 'gray', label: '哀默灰' },
      { value: 'red', label: '党建红' },
    ],
  }),
  actions: {
    getTheme(payload: string) {
      this.theme = payload
      // 获取系统的主题色
      const value = uni.getStorageSync('theme')
      if (value)
        this.theme = value
      else
        uni.setStorageSync('theme', 'light')
    },
    // 修改全局样式
    toggleStyle(payload: string) {
      this.style = payload
      const app = uni.getSystemInfoSync()
      if (payload === 'red') {
        if (app.uniPlatform !== 'mp-weixin') {
          document.getElementsByTagName('body')[0].classList.add('html-red')
          document.getElementsByTagName('body')[0].classList.remove('html-gray')
        }
        else {
          uni.setBackgroundColor({ backgroundColor: 'red' })
        }
      }

      if (payload === 'gray') {
        if (app.uniPlatform !== 'mp-weixin') {
          document.getElementsByTagName('body')[0].classList.add('html-gray')
          document.getElementsByTagName('body')[0].classList.remove('html-red')
        }
      }

      if (payload === 'default') {
        if (app.uniPlatform !== 'mp-weixin') {
          document.getElementsByTagName('body')[0].classList.remove('html-gray')
          document.getElementsByTagName('body')[0].classList.remove('html-red')
        }
      }
    },
    // 主题切换
    toggleTheme() {
      // uni.getSystemInfoSync()
      const value = uni.getStorageSync('theme')
      if (value === 'light') {
        uni.setTabBarStyle({ // 修改底部栏颜色 wx生效
          backgroundColor: '#ffffff',
        })
      }
    },
  },
})
